package com.nhjf.mobile.controller.reviewPainting;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nhjf.common.page.Pagination;
import com.nhjf.mobile.common.util.SysProperties;
import com.nhjf.model.student.Student;
import com.nhjf.model.system.AdminSession;
import com.nhjf.model.system.User;
import com.nhjf.model.teachers.ArtPictures;
import com.nhjf.model.teachers.ArtPicturesComment;
import com.nhjf.model.teachers.ReviewPainting;
import com.nhjf.model.teachers.ReviewPaintingComment;
import com.nhjf.model.teachers.Teacher;
import com.nhjf.pojo.qo.studnet.StudentQo;
import com.nhjf.pojo.qo.system.AdminSessionQo;
import com.nhjf.pojo.qo.system.UserQo;
import com.nhjf.pojo.qo.teachers.ArtPicturesCommentQo;
import com.nhjf.pojo.qo.teachers.ReviewPaintingCommentQo;
import com.nhjf.pojo.qo.teachers.TeachersQo;
import com.nhjf.service.student.StudentService;
import com.nhjf.service.system.AdminSessionService;
import com.nhjf.service.system.UserService;
import com.nhjf.service.teachers.ReviewPaintingCommentService;
import com.nhjf.service.teachers.ReviewPaintingService;
import com.nhjf.service.teachers.TeachersService;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("/rpc")
public class ReviewPaintingCommentController {
	@Autowired
	private ReviewPaintingCommentService reviewPaintingCommentService;
	@Autowired
	private AdminSessionService adminSessionService;
	@Autowired
	private UserService userService;
	@Autowired
	private ReviewPaintingService reviewPaintingService;
	@Autowired
	private TeachersService teachersService;
	@Autowired
	private StudentService studentService;
	/**
	 * 创建改画评论
	 * @param token
	 * @param request
	 * @param model
	 * @param reviewPaintingId
	 * @param comment
	 * @return
	 */
	@RequestMapping("/createReviewPaintingComment")
	@ResponseBody
	public String createReviewPaintingComment(@RequestParam(value="token", required=true)String  token,HttpServletRequest request, Model model,@RequestParam(value="reviewPaintingId", required=true)String reviewPaintingId,@RequestParam(value="comment", required=true)String comment) {
		Map<String, Object> resMap=new HashMap<String, Object>();
		if(StringUtils.isEmpty(token)) {
			resMap.put("code", "500");
			resMap.put("message", "未登录!");
			return JSONObject.fromObject(resMap).toString();
		}
		AdminSessionQo adminSessionQo=new AdminSessionQo();
		adminSessionQo.setToken(token);
		AdminSession adminSession=adminSessionService.queryUnique(adminSessionQo);
		if(adminSession==null) {
			resMap.put("code", "406");
			resMap.put("message", "不存在此账号！");
			return JSONObject.fromObject(resMap).toString();
		}else {
			adminSession.getLoginName();
			UserQo userQo=new UserQo();
			userQo.setUserName(adminSession.getLoginName());
			User user= userService.queryUnique(userQo);
			if(user==null) {

				resMap.put("code", "406");
				resMap.put("message", "未登录!");
				return JSONObject.fromObject(resMap).toString();
			}else {
				if(StringUtils.isEmpty(comment)) {
					resMap.put("code", "500");
					resMap.put("message", "评论不能为空");
					return JSONObject.fromObject(resMap).toString();
				}
				if(StringUtils.isEmpty(reviewPaintingId)) {
					resMap.put("code", "500");
					resMap.put("message", "评论作品不能为空");
					return JSONObject.fromObject(resMap).toString();
				}
				ReviewPainting reviewPainting=reviewPaintingService.get(Long.parseLong(reviewPaintingId));
				if(reviewPainting==null) {
					resMap.put("code", "500");
					resMap.put("message", "不存在此作品");
					return JSONObject.fromObject(resMap).toString();
				}
				ReviewPaintingComment paintingComment=new ReviewPaintingComment();
				paintingComment.setReviewPainting(reviewPainting);
				paintingComment.setComment(comment);
				paintingComment.setCommentTime(new Date());
				paintingComment.setUser(user);
				paintingComment.setCommentType(1);
				TeachersQo teachersQo=new TeachersQo();
				teachersQo.setUserId(user.getId());
				Teacher teacher= teachersService.queryUnique(teachersQo);
				if(teacher==null) {
					paintingComment.setCommentUserType(2);
				}else {
					paintingComment.setCommentUserType(1);
				}
				reviewPaintingCommentService.save(paintingComment);
			}
			resMap.put("code", "200");
			resMap.put("message", "评论成功");
			return JSONObject.fromObject(resMap).toString();
		}
	}
	/**
	 * 作品评论
	 * @param request
	 * @param model
	 * @param reviewPaintingId
	 * @param page
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/queryReviewPaintingComment")
	public String queryReviewPaintingComment(HttpServletRequest request, Model model,@RequestParam(value="reviewPaintingId", required=true) String reviewPaintingId,@RequestParam(value="page", defaultValue="1")Integer page){

		Map<String, Object> resMap=new HashMap<String, Object>();
		Pagination pagination = new Pagination();
		pagination.setPageSize(10);
		ReviewPaintingCommentQo qo=new ReviewPaintingCommentQo();
		qo.setSeachUser(true);
		qo.setReviewPaintingId(Long.parseLong(reviewPaintingId));
		pagination.setCondition(qo);
		pagination.setPageNo(page);
		pagination = reviewPaintingCommentService.queryPagination(pagination);
		List<ReviewPaintingComment> reviewPaintingCommentList=(List<ReviewPaintingComment>)pagination.getList();
		SysProperties sysProperties=SysProperties.getInstance();
		String imagePath=sysProperties.get("imagePath");
		for(ReviewPaintingComment reviewPaintingComment:reviewPaintingCommentList) {
			if(reviewPaintingComment.getUser()!=null) {
				reviewPaintingComment.getUser().setUserPassword("");
			}

			//			SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
			SimpleDateFormat format1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String fom= format1.format(reviewPaintingComment.getCommentTime());
			reviewPaintingComment.setCommentDate(fom);
			if(reviewPaintingComment.getCommentUserType()==1) {
				TeachersQo teacherQo=new TeachersQo();
				teacherQo.setUserId(reviewPaintingComment.getUser().getId());
				Teacher teacher=teachersService.queryUnique(teacherQo);
				if(teacher!=null) {
					reviewPaintingComment.setAvatar(imagePath+"/picture/"+teacher.getAvatar());
					reviewPaintingComment.setNickname(teacher.getNickname());
				}
			}else {
				StudentQo studentQo=new StudentQo();
				studentQo.setUserid(reviewPaintingComment.getUser().getId());
				Student student=studentService.queryUnique(studentQo);
				if(student!=null) {
					reviewPaintingComment.setAvatar(imagePath+"/picture/"+student.getAvatar());
					reviewPaintingComment.setNickname(student.getNickname());
				}
			}

		}
		resMap.put("code", "200");
		resMap.put("message", pagination);
		return JSONObject.fromObject(resMap).toString();
	}
}
